<?php
/**
*+-------第三方平台接入的公共基类--------+
*+-------------------------------------------------+
*+-------------------------------------------------+
*+-@author susy.liang <liangling@ilongyuan.com.cn>-+
*+-------------------------------------------------+
*/
namespace Auth\Event;
use Think\Controller;

abstract class PublicEvent extends Controller {

	/**
	*第三方配置的参数
	*/
	protected $params = array();

	/**
	*第三方应用的appid
	*/
	protected $appId = '';
	
	/**
	*第三方应用的appkey
	*/
	protected $appKey= '';

	/**
	*Authorization Code 请求的地址
	*/
	protected $codeApi = '';

	/**
	*Access Token 请求的地址
	*/
	protected $tokenApi = '';

	/**
	*openid 请求的地址
	*/
	protected $openidApi = '';

	/**
	*user info 请求的地址
	*/
	protected $userApi = '';


	/**
	*回调
	*@param array 请求的参数
	*/
	abstract public function callBack($param=array());

	/**
	 * http 请求 post/get
	 * @author susy.liang 2014/10/20
	 * @param string $url
	 * @param mixed $params
	 * @param string $header
	 * @param string $method
	 * @return mixed
	 */
	protected function doHttp($url, $params = false, $header= false, $method='get'){
		if(is_array($params)){
			$params = http_build_query($params);
		}
		$ch = curl_init();
		if ('GET' == strtoupper($method)) {
			if(!empty($params)) {
				curl_setopt($ch, CURLOPT_URL, $url."?".$params);
			} else {
				curl_setopt($ch, CURLOPT_URL, $url);
			}
		}
		else {
			curl_setopt($ch, CURLOPT_URL, $url);
			curl_setopt($ch, CURLOPT_POST, 1);
			curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
		}

		curl_setopt($ch, CURLOPT_HEADER, false);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
		curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);

		if (is_array($header)) {
			curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
		} else {
			curl_setopt($ch, CURLOPT_HTTPHEADER, array('Expect:'));
		}
		if (stripos($url, 'https://') === 0) {
			curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
			curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
		}
		$result = curl_exec($ch);
		curl_close($ch);
		$result = json_decode($result, true);
		return $result;
	}

	/**
	*第三方用户录入
	*@param array $user 用户信息
	*/
	protected function addUser($user=array()){
			//TODO:add user
	}
}